//
//  LCEssenceCollectionViewCell.m
//  段子手
//
//  Created by dllo on 16/3/23.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LCEssenceCollectionViewCell.h"
#import "NetworkHandle.h"

@interface LCEssenceCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIView *headView;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arrModel;

@end

@implementation LCEssenceCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self create];
        [self dataHandle];
    }
    return self;
}



- (void)create{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, self.contentView.frame.size.height) style:UITableViewStylePlain];
    
    [_tableView registerClass:[LCEssenceTableViewCell class] forCellReuseIdentifier:@"essenceCell"];
    
    _tableView.rowHeight = 100;
    
    
    [self.contentView addSubview:_tableView];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
}


- (void)createHeaderView{
    
            LCEssenceModel *model = _arrModel[0];
    
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENW * model.height / model.width)];
    _tableView.tableHeaderView = _headView;
    
    
    UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENW * model.height / model.width)];

    
    [imgv sd_setImageWithURL:[NSURL URLWithString:model.url]];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
    [_headView addGestureRecognizer:tap];
    
    
    [_headView addSubview:imgv];
    
    
    UILabel *labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(20, _headView.frame.size.height - 80, SCREENW - 40, 80)];
    labelTitle.text = model.title;
    labelTitle.numberOfLines = 3;
    labelTitle.textColor = [UIColor whiteColor];
    labelTitle.backgroundColor = [UIColor clearColor];
    [_headView addSubview:labelTitle];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    LCWebViewController *webVC = [[LCWebViewController alloc]init];
    
    LCEssenceModel *model = _arrModel[0];

    webVC.webStr = model.source_url;
    
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            UIViewController *vv = (UIViewController*)nextResponder;
            
            [vv.navigationController pushViewController:webVC animated:YES];
            
        }
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LCEssenceTableViewCell *essenceCell = [tableView dequeueReusableCellWithIdentifier:@"essenceCell"];
    essenceCell.model = _arrModel[indexPath.row + 1];
    
    return essenceCell;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _arrModel.count - 1;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LCWebViewController *webVC = [[LCWebViewController alloc]init];
    
    LCEssenceModel *model = _arrModel[indexPath.row + 1];
    
    webVC.webStr = model.source_url;
    
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            UIViewController *vv = (UIViewController*)nextResponder;
            
            [vv.navigationController pushViewController:webVC animated:YES];
            
        }
    }
    
}



- (void)dataHandle{
    [WxlAPPTools GETWithURL:@"http://toutiao.com/ma/?media_id=5234320301&count=10&max_behot_time=0&callback=&csrfmiddlewaretoken=undefined&_=1458717352851" par:nil success:^(id responseObject) {
//        NSLog(@"%@", responseObject);
         self.arrModel = [LCEssenceModel modelHandelWithArray:responseObject[@"data"]];
        
        [self createHeaderView];
        [_tableView reloadData];
        
        
    } filed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
    
//    
//[NetworkHandle GETRequestWithURLString:@"http://toutiao.com/ma/?media_id=5234320301&count=10&max_behot_time=0&callback=json&csrfmiddlewaretoken=undefined&_=1458717352851" completion:^(id result) {
////    NSLog(@"%@", result);
//    self.arrModel = [LCEssenceModel modelHandelWithArray:result[@"data"]];
//    
//    NSLog(@"%@", _arrModel);
//    
//}];


    
    
}




@end
